//
//  NetStatusHandle.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/3/29.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "NetStatusHandle.h"
#import "AFNetworking.h"

@implementation NetStatusHandle

+ (NetStatusHandle *)shareHandle
{
    static NetStatusHandle *handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[NetStatusHandle alloc] init];
    });
    return handle;
}

// 保存网络状态
- (void)saveNetStatus:(NSString *)status
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    [myDefault setValue:status forKey:@"netStatus"];
    [myDefault synchronize];
}

// 获取网络状态
- (NSString *)getNetStatus
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    return [myDefault valueForKey:@"netStatus"];
}


// 判断网络状态
- (NSInteger)judgeTheNet
{
    __block NSInteger number;
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    // 开启网络状态监测
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        number = status;
    }];
    return number;
}

// 保存最后一次产品界面搜索的结果
- (void)saveLastResultString:(NSString *)lastString
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    [myDefault setValue:lastString forKey:@"lastResultString"];
    [myDefault synchronize];
}

// 获取最后一次产品界面搜索的结果
- (NSString *)getLastResultString
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    return [myDefault valueForKey:@"lastResultString"];
}

// 保存当前登陆融云的用户ID
- (void)saveCurrentLoginRongCloudUserID:(NSString *)currentUserID
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    [myDefault setValue:currentUserID forKey:@"currentLoginRongYunUserID"];
    [myDefault synchronize];
}

// 取出当前登陆融云用户的ID
- (NSString *)getCurrentLoginRongCloudUserID
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    return [myDefault valueForKey:@"currentLoginRongYunUserID"];
}

// 通过提取当前融云用户的ID
- (NSString *)getCurrentLoginRongCloudUserIDByKey:(NSString *)saveKey
{
    NSArray *tempArray = [saveKey componentsSeparatedByString:@"!"];
    return [tempArray lastObject];
}

// 通过key提取当前用户信息对应的发送者ID
- (NSString *)getCurrentLoginRongCloudUserIDDuiYingSenderIDByKey:(NSString *)saveKey
{
    NSArray *tempArray = [saveKey componentsSeparatedByString:@"!"];
    return [tempArray firstObject];
}

// 通过extra取出发送者姓名
- (NSString *)getSenderNameByGroupExtra:(NSString *)groupExtra
{
    NSArray *tempArray = [groupExtra componentsSeparatedByString:@"!"];
    return [tempArray firstObject];
}

// 通过extra取出群组的姓名
- (NSString *)getGroupNameByGroupExtra:(NSString *)groupExtra
{
    NSArray *tempArray = [groupExtra componentsSeparatedByString:@"!"];
    return [tempArray lastObject];
}

// 获取字符串的空格数
- (NSInteger)getSpaceAndHuanHangCountByString:(NSString *)tempString
{
    // 空格的数量
    NSInteger spaceCount = 0;
    // 换行的数量
    NSInteger huanHangCount = 0;
    for (int i = 0; i < tempString.length; i++) {
        NSString *tempS = [tempString substringWithRange:NSMakeRange(i, 1)];
        if ([tempS isEqualToString:@" "]) {
            spaceCount++;
        }
        if ([tempS isEqualToString:@"\n"]) {
            huanHangCount++;
        }
    }
    return spaceCount + huanHangCount;
}

// 保存最后一次搜索好朋友界面的搜索name
- (void)saveLastSearchFriendName:(NSString *)name
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    [myDefault setValue:name forKey:@"ZGWlastSearchFriendName"];
    [myDefault synchronize];
}

// 取出最后一次搜索好友界面的name
- (NSString *)getLastSearchFriendName
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];;
    return [myDefault valueForKey:@"ZGWlastSearchFriendName"];
}

// 保存我的好友界面的最后一次搜索结果
- (void)saveLastMyFriendResultBySting:(NSString *)string
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    [myDefault setValue:string forKey:@"ZGWLastMyFriendResult"];
    [myDefault synchronize];
}

// 取出我的好友界面的最后一次搜索结果
- (NSString *)getLastMyFriendResult
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];;
    return [myDefault valueForKey:@"ZGWLastMyFriendResult"];
}


// 联系人界面在将要显示的时候是否需要刷新
- (void)setShouldFreshOnLianXiRen:(NSString *)string
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    [myDefault setValue:string forKey:@"ZGWShouldRefreshOnLianXiRen"];
    [myDefault synchronize];
}

// 取出联系人界面是否需要显示
- (NSString *)getShouldFreshOnLianXiRen
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];;
    return [myDefault valueForKey:@"ZGWShouldRefreshOnLianXiRen"];
}

// 保存健康档案界面是否需要刷新
- (void)saveJianKangDangAnShouldRefresh:(NSString *)string
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    [myDefault setValue:string forKey:@"ZGWJianKangDangAnShouldRefresh"];
    [myDefault synchronize];
}

// 取出健康档案界面是否需要刷新
- (NSString *)getJianKangDangAnShouldRefresh
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];;
    return [myDefault valueForKey:@"ZGWJianKangDangAnShouldRefresh"];
}


// 保存融云是否登录成功
- (void)saveLoginRCloudIsSuccess:(NSString *)string
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    [myDefault setValue:string forKey:@"ZGWLoginRCloudIsSuccess"];
    [myDefault synchronize];
}

// 取出融云是否登录成功
- (NSString *)getLoginRCloudIsSuccess
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];;
    return [myDefault valueForKey:@"ZGWLoginRCloudIsSuccess"];
}

// 保存用户是否注册成功--用于提示修改资料
- (void)saveUserRegisterIsSuccess:(NSString *)string
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];
    [myDefault setValue:string forKey:@"ZGWUserRegisterIsSuccess"];
    [myDefault synchronize];
}

// 取出用户是否注册成功
- (NSString *)getUserRegisterIsSucess
{
    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];;
    return [myDefault valueForKey:@"ZGWUserRegisterIsSuccess"];
}

// 自适应宽度
- (CGFloat)adjustWidth:(NSString *)tempString size:(NSInteger)size height:(NSInteger)shouldHeight
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:size * kAdjustParamater] forKey:NSFontAttributeName];
    CGRect frame = [tempString boundingRectWithSize:CGSizeMake(10000, shouldHeight * kAdjustParamater) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.width;
}

// 自适应高度
- (CGFloat)adjustHeight:(NSString *)tempString size:(NSInteger)size width:(NSInteger)shouleWidth
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:size * kAdjustParamater] forKey:NSFontAttributeName];
    CGRect frame = [tempString boundingRectWithSize:CGSizeMake(shouleWidth * kAdjustParamater, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}

// 设置行间距
- (NSMutableAttributedString *)setHangJianJvOfLabel:(NSString *)shouldString lineHeight:(NSInteger) gao
{
    NSMutableAttributedString *tempString = [[NSMutableAttributedString alloc] initWithString:shouldString];
    NSMutableParagraphStyle *paragraghStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraghStyle setLineSpacing:gao * kAdjustParamater];
    [tempString addAttribute:NSParagraphStyleAttributeName value:paragraghStyle range:NSMakeRange(0, shouldString.length)];
    return tempString;
}

// 设置行间距并且设置前几个字大写
- (NSMutableAttributedString *)setHangJianJvOfLabel:(NSString *)shouldString lineHeight:(NSInteger) gao QianJieGeDaXie:(NSInteger)count
{
    NSMutableAttributedString *tempString = [[NSMutableAttributedString alloc] initWithString:shouldString];
    NSMutableParagraphStyle *paragraghStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraghStyle setLineSpacing:gao * kAdjustParamater];
    [tempString addAttribute:NSParagraphStyleAttributeName value:paragraghStyle range:NSMakeRange(0, shouldString.length)];
    // 设置前几个字颜色
    [tempString addAttribute:NSForegroundColorAttributeName value:kCyanColor range:NSMakeRange(7, count)];
    // 设置前几个字大写
    [tempString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22 * kAdjustParamater] range:NSMakeRange(7, count)];
    return tempString;
}

@end
